package cn.dgut.training.Scheduled;

import cn.dgut.training.controller.TransactionService;
import cn.dgut.training.dto.transaction.OrdersDTO;
import cn.dgut.training.emun.GroupRulesStatusEnum;
import cn.dgut.training.emun.GroupStatusEnum;
import cn.dgut.training.entity.activity.GroupBuying;
import cn.dgut.training.entity.activity.GroupBuyingRules;
import cn.dgut.training.entity.transaction.GroupJoin;
import cn.dgut.training.mapper.GroupBuyingMapper;
import cn.dgut.training.mapper.GroupBuyingRulesMapper;
import cn.dgut.training.utils.RestUtil;
import cn.dgut.training.vo.CommonResponse;
import cn.hutool.core.date.DateTime;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
@Service
public class RulesScheduled {

    @Resource
    private GroupBuyingRulesMapper groupBuyingRulesMapper;

    @Resource
    private GroupBuyingMapper groupBuyingMapper;

    @Autowired
    private TransactionService transactionService;

    //处理超时团购规则，自动下架团购活动
    @Scheduled(cron = "0 * * * * ? ")
    public void offlineRules(){
        Date date = DateTime.now();

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR,8);

        Date time = c.getTime();

        QueryWrapper<GroupBuyingRules> rulesQueryWrapper = new QueryWrapper<>();
        rulesQueryWrapper.lt("expire_time",time).eq("status", GroupRulesStatusEnum.RULES_FOR_ONLINE.getValue());

        List<GroupBuyingRules>  groupBuyingRulesList = groupBuyingRulesMapper.selectList(rulesQueryWrapper);

        for(GroupBuyingRules groupBuyingRules:groupBuyingRulesList){
            groupBuyingRules.setStatus(GroupRulesStatusEnum.RULES_FOR_EXPIRELINE.getValue());
            groupBuyingRulesMapper.updateById(groupBuyingRules);
        }
    }

    @Scheduled(cron = "0 * * * * ? ")
    public void offlineActivity(){
        Date date = DateTime.now();

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR,8);

        Date time = c.getTime();

        QueryWrapper<GroupBuying> activityQueryWrapper = new QueryWrapper<>();
        activityQueryWrapper.lt("expire_time",time).eq("status", GroupRulesStatusEnum.RULES_FOR_ONLINE.getValue());

        List<GroupBuying>  groupBuyingList = groupBuyingMapper.selectList(activityQueryWrapper);

        for(GroupBuying groupBuying:groupBuyingList){
            groupBuying.setStatus(GroupStatusEnum.ACTIVITY_FOR_EXPIRELINE.getValue());
            groupBuyingMapper.updateById(groupBuying);

            CommonResponse commonResponse = transactionService.findGroupJoinByActivityId(groupBuying.getId());
            GroupJoin g = (GroupJoin) RestUtil.getObject(commonResponse,GroupJoin.class);

            Long orderId = g.getOrderId();
            transactionService.updateGroupOrderStatus(orderId);
        }
    }
}
